package log;

import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class LogTest {
	private static final Logger log = LoggerFactory.getLogger(LogTest.class);

	@Test
	public void testLogback() {
		Marker timeMarker = MarkerFactory.getMarker("login_success");
		log.info(timeMarker, "XXX用户登录成功！");
		log.debug("debug");
		log.info("info");
		log.warn("warn");
		log.error("error");
	}

	@Test
	public void testLargeLog() {
		long start = System.currentTimeMillis();
		// content大约为1M
		String content =
				IntStream.rangeClosed(1, 100).mapToObj(i -> "b").collect(Collectors.joining("")) + UUID.randomUUID().toString();
		IntStream.rangeClosed(1, 40000).forEach(i -> log.info("{} {}", i, content));
		Marker largeLogMarker = MarkerFactory.getMarker("large_log");
		log.info(largeLogMarker, "1000 took {} ms", System.currentTimeMillis() - start);

/*		long start1 = System.currentTimeMillis();
		IntStream.rangeClosed(1, 10000).forEach(i -> log.info("{} {}", i, content));
		Marker largeLogMarker1 = MarkerFactory.getMarker("large_log");
		log.info(largeLogMarker1, "10000 took {} ms", System.currentTimeMillis() - start1);*/
	}
}
